package gg.com.toolbox.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import gg.com.toolbox.R;
import gg.com.toolbox.receiver.TimeReceiver;
import gg.com.toolbox.tools.BaseActivity;
import gg.com.toolbox.tools.VerifyUser;
import gg.com.toolbox.util.AppConstant;
import gg.com.toolbox.util.MyApplication;
import gg.com.toolbox.view.PickerView;

/**
 * 登录页面
 * Created by gaoge on 2017/10/10.
 */

public class LoginActivity extends BaseActivity {

    //密码界面
    private LinearLayout linearlayout = null;
    //第一位密码
    private PickerView password1 = null;
    //第二位密码
    private PickerView password2 = null;
    //第三位密码
    private PickerView password3 = null;
    //第四位密码
    private PickerView password4 = null;
    //第五位密码
    private PickerView password5 = null;
    //第六位密码
    private PickerView password6 = null;
    //密码控件集合
    private ArrayList<PickerView> picList = new ArrayList<>();
    //动作对象
    private Intent intent = null;
    //时间变化广播
    private TimeReceiver timeReceiver = new TimeReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.login_activity);
        //将页面添加进控制器
        MyApplication.getInstance().addActivity(this);
        //初始化组件
        init();
        //功能实现
        realize();
    }

    /**
     * 初始化组件
     */
    private void init() {
        linearlayout = (LinearLayout) findViewById(R.id.linearlayout);
        password1 = (PickerView) findViewById(R.id.password1);
        password2 = (PickerView) findViewById(R.id.password2);
        password3 = (PickerView) findViewById(R.id.password3);
        password4 = (PickerView) findViewById(R.id.password4);
        password5 = (PickerView) findViewById(R.id.password5);
        password6 = (PickerView) findViewById(R.id.password6);
        picList.add(password1);
        picList.add(password2);
        picList.add(password3);
        picList.add(password4);
        picList.add(password5);
        picList.add(password6);
        //初始化密码
        AppConstant.setData();
        //启动每分钟时间监听
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(timeReceiver, filter);
    }

    /**
     * 功能实现
     */
    private void realize() {
        getPassWord(picList);
    }

    /**
     * 设置密码控件的值
     *
     * @param list
     */
    public void getPassWord(ArrayList<PickerView> list) {
        for (PickerView picView : list) {
            //保存数据的集合
            List<String> data = new ArrayList<>();
            data.add("9");
            for (int i = 0; i < 9; i++) {
                data.add(i + "");
            }
            picView.setData(data);
            picView.setSelected(1);

            picView.setOnSelectListener(new PickerView.onSelectListener() {
                @Override
                public void onSelect(String text) {
                    //验证账号
                    if (new VerifyUser().verify(password1.getData(),password2.getData(),password3.getData(),
                            password4.getData(),password5.getData(),password6.getData())) {
                        ObjectAnimator rotate = ObjectAnimator.ofFloat(linearlayout, "rotation", 0f, 3600f);
                        ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(linearlayout, "alpha", 1f, 0.0f);
                        rotate.setInterpolator(new AccelerateInterpolator());
                        AnimatorSet animSet = new AnimatorSet();
                        animSet.play(rotate).with(fadeInOut);
                        animSet.setDuration(3000);
                        animSet.start();
                        animSet.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                //动画开始的时候调用
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {

                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {
                                //动画被取消的时候调用
                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {
                                //动画重复执行的时候调用

                            }
                        });

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //动画结束的时候调用
                                        intent = new Intent();
                                        intent.setClass(LoginActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                });
                            }
                        }).start();
                    }
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消广播监听
        unregisterReceiver(timeReceiver);
        //将页面从控制器中移除
        MyApplication.getInstance().deleteActivity(this);
    }
}
